package com.lmwyc.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lmwyc.bean.AreaDo;
import com.lmwyc.bean.ExtDayConfig;
import com.lmwyc.bean.UserSession;
import com.lmwyc.service.IAreaService;
import com.lmwyc.service.IEmployeeService;
import com.lmwyc.util.MailUtil;

@Controller
@RequestMapping
public class Helloworld {
	@Autowired
	private IEmployeeService employeeService;
	
	@Autowired
	private IAreaService areaService;
	
	@Autowired
	private MailUtil mail;
	
	@RequestMapping
	@ResponseBody
	public String index() {
		return "欢迎使用自动打卡项目";
	}
	
	@RequestMapping(value="/hello",method= {RequestMethod.GET})
	@ResponseBody
	public String helloworld() {
		return "hello world";
	}
	
	@RequestMapping(value="/index",method= {RequestMethod.GET})
	public String index(HttpServletRequest request,String workNum) {
		return "redirect/index";
	}
	
	@RequestMapping(value="/index2",method= {RequestMethod.GET})
	public String index2() {
		return "redirect/index2";
	}
	
	@ResponseBody
	@RequestMapping("sendEmail")
	public String sendEmail() {
		mail.sendSimpleMail("743377139@qq.com", "测试网络", "测试网络");
		return "ok";
	}
	
	@RequestMapping("/set/{workNum}")
	public String setInfo(@PathVariable String workNum, Model model) {
		UserSession session = employeeService.getUser(workNum);
		if (session == null) {
			addUser(workNum);
			session = employeeService.getUser(workNum);
		}
		model.addAttribute("user", session);
		List<ExtDayConfig> list = employeeService.getCusConf(workNum);
		String object = JSONObject.toJSONString(list);
		model.addAttribute("conf", object);
		return "set/index";
	}
	
	/**
	 * 增加
	 * 
	 * @param workNum
	 */
	private void addUser(String workNum) {
		if (workNum == null || workNum.trim().length() != 10 || !StringUtils.isNumber(workNum)) {
			throw new RuntimeException("工号错误，请输入正确的十位lob工号");
		}
		employeeService.addUser(workNum);
	}
	
	@RequestMapping("/addConf/{userId}/{datePick}/{isPunsh}")
	@ResponseBody
	public String addConf(@PathVariable String userId, @PathVariable String datePick, @PathVariable int isPunsh) {
		if(userId != null && userId.trim().length() > 0 && datePick != null && datePick.trim().length() > 0) {
			employeeService.addConf(userId, datePick, isPunsh);
		}
		return "OK";
	}
	
	@RequestMapping("/delConf/{confId}")
	@ResponseBody
	public String delConf(@PathVariable String confId) {
		employeeService.delConf(confId);
		return "OK";
	}
	
	/**
	 * 2020年5月11日 系统更新，无法获取到全国数据
	 * @param onwershipPlace
	 * @param office
	 * @param lat
	 * @param lon
	 * @param page
	 * @param limit
	 * @return
	 * @throws IOException
	 */
//	@RequestMapping("/saveArea")
//	@ResponseBody
//	public String saveArea() throws IOException {
//		areaService.saveAreas();
//		return "OK";
//	}
	
	@RequestMapping("/areaSearch")
	@ResponseBody
	public Map<String, Object> areaSearch(String onwershipPlace, String office, Double lat, Double lon, Integer page, Integer limit) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", 0);
		result.put("msg", 0);
		List<AreaDo> list = null;
		if(lat != null && lon != null && lat != 0 && lon != 0) {
			list = areaService.list(null, onwershipPlace, office, lat, lon);
			result.put("count", list.size());
			result.put("data", list);
		}else {
			PageHelper.startPage(page, limit);
			list = areaService.list(null, onwershipPlace, office, 0, 0);
			PageInfo<AreaDo> pageInfo = new PageInfo<AreaDo>(list);
			result.put("count", pageInfo.getTotal());
			result.put("data", pageInfo.getList());
		}
		return result;
	}
	
	@RequestMapping("/changeArea/{userId}/{areaId}")
	@ResponseBody
	public String changeArea(@PathVariable String userId, @PathVariable Integer areaId) throws IOException {
		if (areaId == null) {
			return "failed";
		}
		List<AreaDo> list = areaService.list(areaId, null, null, 0, 0);
		if (list == null || list.size() < 1) {
			return "failed";
		}
		employeeService.updateArea(userId, list.get(0));
		return "OK";
	}
	
	@RequestMapping("/changeName/{userId}/{userName}")
	@ResponseBody
	public String changeName(@PathVariable String userId, @PathVariable String userName) throws IOException {
		if (userName == null || "".equals(userName)) {
			return "failed";
		}
		employeeService.updateName(userId, userName);
		return "OK";
	}
	
	@RequestMapping("/changeMail/{userId}/{mailAddress}")
	@ResponseBody
	public String changeMail(@PathVariable String userId, @PathVariable String mailAddress) throws IOException {
		if (mailAddress == null || "".equals(mailAddress)) {
			return "failed";
		}
		employeeService.updateMail(userId, mailAddress);
		return "OK";
	}

	@RequestMapping("/changeTime/{userId}/{time}")
	@ResponseBody
	public String changeTime(@PathVariable String userId, @PathVariable Integer time) throws IOException {
		if (time == null || time <= 0) {
			return "failed";
		}
		employeeService.updateTime(userId, time);
		return "OK";
	}
}
